package com.hashwolf.newpipe.util;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;

import com.hashwolf.newpipe.R;

import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;

import java.util.Optional;

public class ServiceHelper {

    private static final StreamingService DEFAULT_FALLBACK_SERVICE = ServiceList.YouTube;

    public static int getSelectedServiceId(final Context context) {
        return Optional.ofNullable(getSelectedService(context)).orElse(DEFAULT_FALLBACK_SERVICE).getServiceId();
    }

    @Nullable
    public static StreamingService getSelectedService(final Context context) {
        final String serviceName = PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.current_service_key), context.getString(R.string.default_service_value));

        try {
             return NewPipe.getService(serviceName);
        } catch (final ExtractionException e) {
            return null;
        }
    }
}
